package com.endoc.designpattern.singleton;

/**
 * 懒汉模式,双重检查
 */
public class SluggardSingleton {
    SluggardSingleton sluggardSingleton;

    public SluggardSingleton getInstance(){
        if(sluggardSingleton==null){//此处执行时,可能判断为null后,cpu就失去了执行权,然后别的线程去创建了SluggardSingleton对象,所以需要两次判断
            synchronized (SluggardSingleton.class){
                if(sluggardSingleton==null){
                    sluggardSingleton = new SluggardSingleton();
                }
            }
        }
        return sluggardSingleton;
    }
}
